﻿using System;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.User;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.SecurityConsole.User
{
    public partial class UserRole : System.Web.UI.BasePage
    {
        private UserService _userServie;
        private UserService UserService
        {
            get
            {
                return _userServie ??
                       (_userServie = ObjectHelper.GetObject<UserService>());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SearchGrid(object sender, EventArgs e)
        {
            SmartGridView1.PageIndex = 0;
            SmartGridView1.DataBind();
        }

        protected void RemoveRoleFromUser(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton) sender;
            string roleId = img.Attributes["_KeyId"];
            string userId = Request.Params["UserId"];

            this.UserService.DeleteRolesFromUser(userId, new string[] { roleId });
            SmartGridView1.DataBind();
        }

        protected void RemoveRolesFromUser(object sender, EventArgs e)
        {
            string userId = Request.Params["UserId"];
            string[] roleIds = SmartGridView1.GetCheckedValues();

            this.UserService.DeleteRolesFromUser(userId, roleIds);
            SmartGridView1.DataBind();
        }

        protected void AddRoleIntoUser(object sender, EventArgs e)
        {
            string userId = Request.Params["UserId"];
            string[] roleIds = SmartGridView1.GetCheckedValues();

            this.UserService.AddRolesForUser(userId, roleIds);
            SmartGridView1.PageIndex = 0;
            SmartGridView1.DataBind();
        }
    }
}